layui.config({
    base:'layuicms/common/js/'//定义基目录(相对于页面的所在目录,这里login.html刚好定义了base标签)，自定义扩展模块是根据这个目录来找的
}).extend({
    ajaxExtention:'ajaxExtention', //加载自定义扩展
    $tool:'tool',
    $sha1:'../../lib/sha1/sha1'
}).use(['form', 'layer','ajaxExtention','$tool','$sha1'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery,
        $sha1 = layui.$sha1,
        $tool=layui.$tool;

    //video背景
    $(window).resize(function () {
        if ($(".video-player").width() > $(window).width()) {
            $(".video-player").css({
                "height": $(window).height(),
                "width": "auto",
                "left": -($(".video-player").width() - $(window).width()) / 2
            });
        } else {
            $(".video-player").css({
                "width": $(window).width(),
                "height": "auto",
                "left": -($(".video-player").width() - $(window).width()) / 2
            });
        }
    }).resize();

    //登录按钮事件
    form.on("submit(login)", function (data) {
        console.log(data.field);
        var req = {
            name: data.field.username,
            pwd: $sha1.hex_sha1(data.field.password),
            code: data.field.code
        };

       //请求登录
       $.ajax({
           url:$tool.getContext()+ "login",
           data:req,
           method:"post",
           success:function (data) {
               //保存用户信息和应用上下文到session中
               window.sessionStorage.setItem("sysUser",data.data.loginName);
               window.sessionStorage.setItem("userId",data.data.userId);
               window.sessionStorage.setItem("context",data.data.webContext);
               //登录成功跳转到首页,code !== '0000'的已经在ajaxExtention中统一处理了
               window.location.href = $tool.getContext()+"layuicms/index.html";
           }
       });

        return false;
    });

    /**
     * 更换验证码
     * */
    $('.code img').click(function () {
        this.src = $tool.getContext() + 'getVCode';
    });

});
